package com.hzsparrow.ai.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzsparrow.ai.entity.ChatHistory;
import com.hzsparrow.ai.mapper.ChatHistoryMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * 聊天历史服务类
 */
@Service
public class ChatHistoryService extends ServiceImpl<ChatHistoryMapper, ChatHistory> {

    /**
     * 保存聊天记录
     *
     * @param chatHistory 聊天记录
     * @return 保存结果
     */
    public boolean saveChatHistory(ChatHistory chatHistory) {
        if (chatHistory.getSessionId() == null || chatHistory.getSessionId().isEmpty()) {
            chatHistory.setSessionId(UUID.randomUUID().toString());
        }
        chatHistory.setCreateTime(LocalDateTime.now());
        return this.save(chatHistory);
    }

    /**
     * 查询用户的所有聊天会话
     *
     * @param userId 用户ID
     * @return 会话ID列表
     */
    public List<String> listUserSessions(Long userId) {
        return this.baseMapper.selectList(
                new LambdaQueryWrapper<ChatHistory>()
                        .select(ChatHistory::getSessionId)
                        .eq(ChatHistory::getUserId, userId)
                        .groupBy(ChatHistory::getSessionId)
                        .orderByDesc(ChatHistory::getCreateTime)
            )
            .stream()
            .map(ChatHistory::getSessionId)
            .distinct()
            .toList();
    }

    /**
     * 查询特定会话的聊天历史
     *
     * @param sessionId 会话ID
     * @param page 页码
     * @param size 每页大小
     * @return 聊天历史
     */
    public Page<ChatHistory> listSessionHistory(String sessionId, int page, int size) {
        Page<ChatHistory> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<ChatHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatHistory::getSessionId, sessionId);
        queryWrapper.orderByAsc(ChatHistory::getCreateTime);
        
        return this.page(pageParam, queryWrapper);
    }

    /**
     * 查询用户的最近聊天记录
     *
     * @param userId 用户ID
     * @param limit 记录数量
     * @return 聊天记录
     */
    public List<ChatHistory> listRecentHistory(Long userId, int limit) {
        LambdaQueryWrapper<ChatHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatHistory::getUserId, userId);
        queryWrapper.orderByDesc(ChatHistory::getCreateTime);
        queryWrapper.last("LIMIT " + limit);
        
        return this.list(queryWrapper);
    }

    /**
     * 根据知识库ID查询聊天记录
     *
     * @param knowledgeBaseId 知识库ID
     * @param page 页码
     * @param size 每页大小
     * @return 聊天记录
     */
    public Page<ChatHistory> listByKnowledgeBase(Long knowledgeBaseId, int page, int size) {
        Page<ChatHistory> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<ChatHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatHistory::getKnowledgeBaseId, knowledgeBaseId);
        queryWrapper.orderByDesc(ChatHistory::getCreateTime);
        
        return this.page(pageParam, queryWrapper);
    }

    /**
     * 删除会话历史
     *
     * @param sessionId 会话ID
     * @return 删除结果
     */
    public boolean deleteSessionHistory(String sessionId) {
        LambdaQueryWrapper<ChatHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatHistory::getSessionId, sessionId);
        
        return this.remove(queryWrapper);
    }
} 